package com.wuhongyu.controller;

import com.wuhongyu.entity.Dictionary;
import com.wuhongyu.service.DictionaryService;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 字典
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;


    @RequestMapping("/list")
    public String list(Model model, HttpServletRequest request, Dictionary dictionary, Integer number) {
        model.addAttribute("url",MyUtil.getUrl(request));
        model.addAttribute("page",dictionaryService.findAll(dictionary,number));
        return "~basd/dict";
    }


    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id) {
        if(id !=null){
            model.addAttribute("dict",dictionaryService.findOne(id));
            return "~basd/dict_edit";
        }
        return "~basd/dict_add";
    }


    @RequestMapping("/save")
    public String save(Model model,HttpServletRequest request, Dictionary dictionary) {
        dictionaryService.save(dictionary);
        return "redirect:/dictionary/list";
    }


    @RequestMapping("/delete")
    public String delete(Model model,HttpServletRequest request, Integer id) {
        dictionaryService.delete(id);
        return "redirect:/dictionary/list";
    }
}